public class Test {

    private int sum(int n) {
        int count = 0;
        while(n != 0) {
            int x = n % 10;
            count += x*x;
            n /= 10;
        }
        return count;
    }

    public boolean isHappy(int n) {
        int slow = n;
        int fast = sum(n);
        while(slow != fast) {
            slow = sum(slow);
            fast = sum(sum(fast));
        }
        return slow == 1;
    }

}
